﻿Imports System.IO

Public Class frmMain

  ''' <summary>
  ''' Loads the computer information from the Options file into memory
  ''' </summary>
  Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Hide the form
    Me.Visible = False

    Try
      'Check to see if another instance of program is running, and if so close this instance
      Dim appProc() As Process = Process.GetProcessesByName(My.Application.Info.ProductName)
      If appProc.Length > 1 Then
        Application.Exit()
      End If

      'Get the information regarding the Options file
      Dim f As New FileInfo(Application.StartupPath & "\dsrOptions.xml")

      If f.Exists Then     'If the file exists...
        'Load the Computers arraylist with the computers from the options file to check
        LoadComputersCollection(f.FullName)
        'Check the computers in the Computers arraylist
        CheckSizes()
        'Enable the timer to check the computers in the Computers arraylist at the specified interval
        Me.tmrDiskSpaceReporter.Enabled = True
      Else     'If the options file doesn't exist...
        'Ask the user if they wish to create the Default options file and if so...
        If MessageBox.Show("Unable to locate the DiskSpaceReporter Options file. Would you like to create the default file now?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
          If CreateOptionsFile() = True Then     'If the creation of the Default options file is successful, then...
            'Load the Computers arraylist with the computers from the options file to check
            LoadComputersCollection(f.FullName)
            'Check the computers in the Computers arraylist
            CheckSizes()
            'Set the timer interval and enable the timer to check the computers in the Computers arraylist at the specified interval
            Me.tmrDiskSpaceReporter.Interval = intPollingInterval
            Me.tmrDiskSpaceReporter.Enabled = True
            Me.tmrDiskSpaceReporter.Start()
          Else     'If the creation of the Default options file fails, exit the program
            MessageBox.Show("Unable to create the default DiskSpaceReporter Options file. The program will now exit.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
            Application.Exit()
          End If
        Else     'If the user does not want to create the Default options file, exit the program
          MessageBox.Show("Unable to load DiskSpaceReporter Options file. The program will now exit.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
          Application.Exit()
        End If
      End If
    Catch ex As Exception
      MessageBox.Show("There was an unexpected error in the program. The program will now exit.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1)
      Application.Exit()
    End Try
  End Sub

  ''' <summary>
  ''' Checks the sizes of the drives listed for the computers in the Computers arraylist
  ''' </summary>
  Private Sub CheckNowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckNowToolStripMenuItem.Click
    'Check the sizes of the drives listed for the computers in the Computers arraylist
    CheckSizes()
  End Sub

  ''' <summary>
  ''' Opens the Options window
  ''' </summary>
  Private Sub OptionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptionsToolStripMenuItem.Click
    'Open the Options window
    frmOptions.Show()
  End Sub

  Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
    frmAbout.Show()
  End Sub

  ''' <summary>
  ''' Exits the program
  ''' </summary>
  Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    'Exit the program
    Application.Exit()
  End Sub

  ''' <summary>
  ''' Checks the sizes of the drives listed for the computers in the Computers arraylist when the timer interval reaches 0
  ''' </summary>
  Private Sub tmrDiskSpaceReporter_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrDiskSpaceReporter.Tick
    'Check the sizes of the drives listed for the computers in the Computers arraylist
    CheckSizes()
  End Sub

End Class
